package com.echo.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.echo.SportsApplication;
import com.echo.dialog.EditDialog;
import com.echo.http.HttpCallBack;
import com.echo.http.HttpManager;
import com.echo.http.HttpUrl;
import com.echo.util.PasswordUtil;
import com.echo.util.ToastUtils;
import com.echo.view.RoundedImageView;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class PersonalActivity extends AppCompatActivity {

    @InjectView(R.id.m_personal_username)
    TextView mPersonalUsername;
    @InjectView(R.id.m_personal_nickname)
    TextView mPersonalNickname;
    @InjectView(R.id.toolbar_title)
    TextView toolbarTitle;
    @InjectView(R.id.toolbar)
    Toolbar toolbar;
    @InjectView(R.id.m_personal_head_image)
    RoundedImageView mPersonalHeadImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal);
        ButterKnife.inject(this);
        mPersonalNickname.setText(SportsApplication.getInstance().getUserNickname());
        mPersonalUsername.setText(SportsApplication.getInstance().getUsername());
        
    }


    public void modify_nickname(View view) {
        final EditDialog dialog = new EditDialog(this);
        dialog.setTitle("修改昵称");
        dialog.setLine1("新昵称", "", false);
        dialog.setPositiveButton("", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = HttpUrl.URL_MODIFY_NICKNAME + "?id=" + SportsApplication.getInstance().getUserId() + "&nickname=" + dialog.getText1();
                final String nickname = dialog.getText1();
                HttpManager.getInstance().modify_nickname(url, new HttpCallBack() {
                    @Override
                    public void onMsg(Object response) throws JSONException {
                        JSONObject jsonObject = (JSONObject) response;
                        ToastUtils.show(PersonalActivity.this, (CharSequence) jsonObject.get("message"), Toast.LENGTH_SHORT);
                        if (jsonObject.getString("message").equals("SUCCESS"))
                            SportsApplication.getInstance().getUser().setNickname(nickname);
                    }

                    @Override
                    public void onError() {

                    }
                });
            }
        }).setNegativeButton("", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        }).show();
    }

    public void modify_password(View view) {
        final EditDialog dialog = new EditDialog(this);
        dialog.setTitle("修改密码");
        dialog.setLine1("旧密码", "", true);
        dialog.setLine2("新密码", "", true);
        dialog.setLine3("确认密码", "", true);
        dialog.setPositiveButton("", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String text1 = dialog.getText1(), text2 = dialog.getText2(), text3 = dialog.getText3();
                if (text2.equals(text3)) {

                    String url = HttpUrl.URL_MODIFY_PASSWORD + "?id=" + SportsApplication.getInstance().getUserId() + "&oldPassword=" + PasswordUtil.getBase64(text1) + "&newPassword=" + PasswordUtil.getBase64(text2);

                    HttpManager.getInstance().modify_nickname(url, new HttpCallBack() {
                        @Override
                        public void onMsg(Object response) throws JSONException {
                            JSONObject jsonObject = (JSONObject) response;
                            ToastUtils.show(PersonalActivity.this, jsonObject.getString("message"), Toast.LENGTH_SHORT);
                            if (jsonObject.getString("message").equals("SUCCESS"))
                                SportsApplication.getInstance().getUser().setPassword(text2);
                        }

                        @Override
                        public void onError() {

                        }
                    });
                } else {
                    ToastUtils.show(PersonalActivity.this, "密码确认不一致", Toast.LENGTH_SHORT);
                }
            }
        }).setNegativeButton("", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        }).show();
    }

    public void logout(View view) {
        SportsApplication.logout();
        finish();
    }
}
